package com.yfqy.admin.resource;

import com.github.pagehelper.PageInfo;
import com.yfqy.admin.domain.dto.attribute.AddAttribute;
import com.yfqy.admin.domain.dto.attribute.AttributeDTO;
import com.yfqy.admin.domain.dto.attribute.AttributeQuery;
import com.yfqy.admin.domain.dto.attribute.ModifiedAttribute;
import com.yfqy.admin.service.AttributeService;
import com.yfqy.admin.domain.vo.common.ResultVO;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 属性字段管理
 */
@RestController
@RequestMapping("/api/internal/attribute")
public class AttributeController {

    @Resource
    private AttributeService attributeService;

    /**
     * 属性详情
     *
     * @param queryById
     * @return
     */
    @GetMapping({"/selectById"})
    public ResultVO<AttributeDTO> selectById(@RequestParam("id") Long queryById) {
        AttributeDTO attributeDTO = attributeService.selectByPrimaryKey(queryById);
        return ResultVO.data(attributeDTO);
    }

    /**
     * 属性分页列表
     *
     * @param attributeQuery
     * @return
     */
    @PostMapping("/pageListByNameAndType")
    public ResultVO<PageInfo<AttributeDTO>> pageListByNameAndType(@RequestBody AttributeQuery attributeQuery) {
        return ResultVO.data(attributeService.pageListByNameAndType(attributeQuery));
    }

    /**
     * 添加属性
     *
     * @param addAttribute
     * @return
     */
    @PostMapping("/add")
    public ResultVO<String> addAttribute(@RequestBody @Validated AddAttribute addAttribute) {
        int insert = attributeService.addAttribute(addAttribute);
        return insert > 0 ? ResultVO.success("添加属性成功") : ResultVO.fail("添加属性失败");
    }


    /**
     * 修改属性
     *
     * @param modifyAttribute
     * @return
     */
    @PostMapping("/modify")
    public ResultVO<String> modifyAttribute(@RequestBody @Validated ModifiedAttribute modifyAttribute) {
        int modified = attributeService.modifyAttribute(modifyAttribute);
        return modified > 0 ? ResultVO.success("修改属性成功") : ResultVO.fail("修改属性失败");
    }


}
